import org.apache.commons.lang.ArrayUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by sToa on 2014/12/29.
 */
public class SetEventAttr {

    public static String[] PROTOCOL_NAME = {"HOPOPT","ICMP","IGMP","GGP","IP","ST","TCP","CBT","EGP","IGP","BBN-RCC-MON","NVP-II","PUP","ARGUS","EMCON","XNET","CHAOS","UDP","MUX","DCN-MEAS","HMP","PRM","XNS-IDP","TRUNK-1","TRUNK-2","LEAF-1","LEAF-2","RDP","IRTP","ISO-TP4","NETBLT","MFE-NSP","MERIT-INP","SEP","3PC","IDPR","XTP","DDP","IDPR-CMTP","TP++","IL","IPv6","SDRP","IPv6-Route","IPv6-Frag","IDRP","RSVP","GRE","MHRP","BNA","ESP","AH","I-NLSP","SWIPE","NARP","MOBILE","TLSP","SKIP","IPv6-ICMP","IPv6-NoNxt","IPv6-Opts","CFTP","SAT-EXPAK","KRYPTOLAN","RVD","IPPC","SAT-MON","VISA","IPCV","CPNX","CPHB","WSN","PVP","BR-SAT-MON","SUN-ND","WB-MON","WB-EXPAK","ISO-IP","VMTP","SECURE-VMTP","VINES","TTP","NSFNET-IGP","DGP","TCF","EIGRP","OSPFIGP","Sprite-RPC","LARP","MTP","AX.25","IPIP","MICP","SCC-SP","ETHERIP","ENCAP","GMTP","IFMP","PNNI","PIM","ARIS","SCPS","QNX","A/N","IPComp","SNP","Compaq-Peer","IPX-in-IP","VR","PGM","L2TP","DDX","IATP","STP","SRP","UTI","SMP","SM","PTP","ISIS","FIRE","CRTP","CRUDP","SSCOPMCE","IPLT","SPS","PIPE","SCTP","FC"};
    public static int[] PROTOCOL_NO = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,62,64,65,66,67,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,100,101,102,103,104,105,106,107,108,109,110,111,112,113,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133};

    public String toDateTime(String[] paramArr)
    {
        if(paramArr.length == 3)
        {
            return paramArr[0] + "-" + paramArr[1] + " " + paramArr[2];
        }
        return paramArr[2] + "-" + paramArr[0] + "-" + paramArr[1] + " " + paramArr[3];
    }

    public String combineMsgId(String[] paramArr)
    {
        String combineStr = "";
        for(String str: paramArr)
        {
            combineStr += str;
        }
        return combineStr;
    }

    public String replaceStrInStr(String[] paramArr)
    {
        return paramArr[0].replaceAll(paramArr[1], paramArr[2]);
    }

    public String add(String[] paramArr)
    {
        long addVal = 0;
        for(String str: paramArr)
        {
            addVal += Long.valueOf(str);
        }
        return String.valueOf(addVal);
    }

    public int convertStrToIntIpPort(String[] paramArr)
    {
        return Integer.valueOf(paramArr[0]);
    }

    public int convertHexStrToInt(String[] paramArr)
    {
        return Integer.valueOf(paramArr[0].replace("0x", ""), 16);
    }

    public long calculateMSec(String[] paramArr)
    {
        String[] timeArr = paramArr[0].split(":");
        return (Long.valueOf(timeArr[0])*3600 + Long.valueOf(timeArr[1])*60 + Long.valueOf(timeArr[2]))*1000;
    }

    public String replaceStringByRegex(String[] paramArr)
    {
        return paramArr[0].replaceAll(paramArr[1], paramArr[2]);
    }

    public String compIntfSecVal(String[] paramArr)
    {
        System.out.printf(paramArr[0] + " " + paramArr[1]);
        return null;
    }

    public int convertStrToIntIpProto(String[] paramArr)
    {
        int index = ArrayUtils.indexOf(PROTOCOL_NAME, paramArr[0]);
        if(index >= 0)
        {
            return PROTOCOL_NO[index];
        }
        return -1;
    }

    public long scale(String[] paramArr)
    {
        return Long.valueOf(paramArr[0]) * Long.valueOf(paramArr[1]);
    }

    public String shiftTimeSec(String[] paramArr)
    {
        String dateTimeStr = paramArr[2] + "-" + paramArr[0] + "-" + paramArr[1] + " " + paramArr[3];
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            long dateTime = sdf.parse(dateTimeStr).getTime();
            long durationSec = Long.valueOf(paramArr[4]) * 1000;
            dateTimeStr = (dateTime - durationSec) + "";
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return sdf.format(Long.valueOf(dateTimeStr));
    }

    public String convertIpDecimalToStr(String[] paramArr)
    {
        StringBuilder sb = new StringBuilder(15);
        for(String str: paramArr)
        {
            long ip = Long.valueOf(str);
            for (int i = 0; i < 4; i++)
            {
                sb.insert(0, Long.toString(ip & 0xff));
                if (i < 3)
                {
                    sb.insert(0, '.');
                }
                ip = ip >> 8;
            }
            sb.insert(0, ", ");
        }
        return sb.delete(0,2).toString();
    }

    public String normalizeMAC(String[] paramArr)
    {
        String macAddr = paramArr[0].replaceAll("\\.|:|-", "");
        String newMacAddr = "";
        for(int i=0; i<macAddr.length(); i++)
        {
            if(i>0 && i%2==0)
            {
                newMacAddr += ":" + macAddr.charAt(i);
            } else
            {
                newMacAddr += macAddr.charAt(i);
            }
        }
        return newMacAddr;
    }

    public String extractHostFromFQDN(String[] paramArr)
    {
        String regex = "([a-z]+\\.{1}[a-z]+)";
        Pattern p  = Pattern.compile(regex);
        Matcher m = p.matcher(paramArr[0]);
        String hostName = "";
        while(m.find())
        {
            hostName = m.group();
        }
        return hostName;
    }

    public String trimAttribute(String[] paramArr)
    {
        return ParserHelper.trim(paramArr[0], paramArr[1]);
    }

    public static void main(String[] args) {
        System.out.println(new SetEventAttr().convertIpDecimalToStr(new String[]{"3232235778", "4232235780"}));
    }

}
